package com.Xht.www.servlet;

//后端接收用户申请入住请求

import com.Xht.www.dao.UserHistoryOrdersDAO;
import com.Xht.www.po.UserHistoryOrdersPO;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/applyCheckIn")
public class ApplyCheckInServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        int userId = Integer.parseInt(request.getParameter("userId"));

        UserHistoryOrdersDAO ordersDAO = new UserHistoryOrdersDAO();
        UserHistoryOrdersPO order = ordersDAO.getOrderById(orderId);

        if (order != null && order.getUserId() == userId) {
            // 新增订单状态检查逻辑
            if (!"正常".equals(order.getOrderStatus())) {
                response.getWriter().write("当前订单状态不允许申请入住，请确认订单状态为正常");
                return;
            }

            order.setOrderStatus("申请中");
            if (ordersDAO.updateOrder(order)) {
                response.getWriter().write("申请入住成功");
            } else {
                response.getWriter().write("申请入住失败，请重试");
            }
        } else {
            response.getWriter().write("未找到对应的订单或用户信息");
        }
    }
}